package com.jrauto.CarAppBackend.controller;

/**
 * @author : huiMing
 * Date : 2025年06月27日 10:55
 * @version V1.0
 */

import com.jrauto.CarAppBackend.model.Brand;
import com.jrauto.CarAppBackend.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌管理 RESTful API 控制器
 */
@Tag(name = "BrandController", description = "汽车品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    /**
     * 新增品牌
     * POST /api/brand
     * @param brand 品牌实体对象
     * @return ResponseEntity 包含操作结果
     */
    @Operation(summary = "新增汽车品牌", description = "提交一个新的汽车品牌记录")
    @PostMapping
    public ResponseEntity<String> addBrand(@RequestBody Brand brand) {
        try {
            boolean success = brandService.addBrand(brand);
            if (success) {
                return ResponseEntity.status(HttpStatus.CREATED).body("品牌添加成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("品牌添加失败");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取品牌详情
     * GET /api/brand/{id}
     * @param id 品牌ID
     * @return ResponseEntity 包含品牌实体或错误信息
     */
    @Operation(summary = "根据ID获取品牌详情", description = "根据品牌ID查询单条品牌记录")
    @GetMapping("/{id}")
    public ResponseEntity<Brand> getBrandById(@PathVariable Long id) {
        try {
            Brand brand = brandService.getBrandById(id);
            if (brand != null) {
                return ResponseEntity.ok(brand);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 获取所有品牌列表
     * GET /api/brand
     * @return ResponseEntity 包含品牌列表或错误信息
     */
    @Operation(summary = "获取所有品牌列表", description = "查询所有汽车品牌记录")
    @GetMapping("/getAll")
    public ResponseEntity<List<Brand>> getAllBrands() {
        try {
            List<Brand> brands = brandService.getAllBrands();
            return ResponseEntity.ok(brands);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 根据品牌名称获取品牌列表
     * GET /api/brand/name/{name}
     * @param name 品牌中文名
     * @return ResponseEntity 包含品牌列表或错误信息
     */
    @Operation(summary = "根据名称获取品牌列表", description = "根据品牌中文名查询品牌记录")
    @GetMapping("/name/{name}")
    public ResponseEntity<List<Brand>> getBrandsByName(@PathVariable String name) {
        try {
            List<Brand> brands = brandService.getBrandsByName(name);
            return ResponseEntity.ok(brands);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 更新品牌信息
     * PUT /api/brand/{id}
     * @param id 品牌ID
     * @param brand 更新后的品牌实体
     * @return ResponseEntity 包含操作结果
     */
    @Operation(summary = "更新品牌信息", description = "更新指定ID的汽车品牌信息")
    @PutMapping("/{id}")
    public ResponseEntity<String> updateBrand(@PathVariable Integer id, @RequestBody Brand brand) {
        try {
            brand.setId(id);
            boolean success = brandService.updateBrand(brand);
            if (success) {
                return ResponseEntity.ok("品牌更新成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("品牌更新失败，可能ID不存在或数据异常");
            }
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 删除品牌
     * DELETE /api/brand/{id}
     * @param id 品牌ID
     * @return ResponseEntity 包含操作结果
     */
    @Operation(summary = "删除品牌", description = "根据ID删除一个汽车品牌")
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteBrand(@PathVariable Long id) {
        try {
            boolean success = brandService.deleteBrand(id);
            if (success) {
                return ResponseEntity.ok("品牌删除成功");
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("品牌删除失败，品牌不存在");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
        }
    }
}

